import requests
import re
import time
import random
def pa(url):
    f1=open("httpreq.txt","r")
    t1=f1.read().split("\n")
    f1.close()
    t2=[]
    headers={}
    for i in t1[1:]:
        try:
            headers[i.split(":")[0]]=i.split(":")[1].strip(" ")
        except:
            ''
    proxy ='127.0.0.1:1080'
    proxies = {"http": "http://%(proxy)s/" % {'proxy': proxy},"https": "http://%(proxy)s/" % {'proxy': proxy}}
    s = requests.session()
    req = s.get(url,headers=headers,proxies=proxies)
    print(req)
    f1=open("req.txt","w",encoding="UTF-8")
    req=req.text
    f1.write(req)
    f1.close()
pq=input("是否需要爬取数据?(输入T爬取):").upper()
if(pq=="T"):
    if(input("该批量爬取为老年人爬法，不然会被封，挂着慢慢来\n是否需要进行批量爬取?(T/F，默认不启用)").upper()=="T"):
        zu=input("请输入需要批量爬取的组(不知道就写A):").upper()
        with open("jgb"+zu+".txt","r") as f:
            f1=f.readlines()
        input("确保httpreq.txt为最新以后按Enter继续...")
        for i in f1:
            na=i.split("=")[1].split("\"")[1]
            print("开始爬取:"+na)
            url="https://steamcommunity.com/market/search?appid=730&q="+na
            pa(url)
            print(na+"爬取已结束")
            sj=random.randint(29,37)
            print("代码休息"+str(sj)+"秒后继续爬取")
            time.sleep(sj)
    else:
        zwurl=input("请输入你需要爬取的枪械名称(例:MP9|飞驰):")
        url="https://steamcommunity.com/market/search?appid=730&q="+zwurl
        input("确保httpreq.txt为最新以后按Enter继续...")
        pa(url)
#正则过滤爬取内容 jiexi  return jg
f1=open("req.txt","r",encoding="UTF-8")
f1txt=f1.read()
f1.close()
pattern = re.compile(r'(?<=>)[^\n].+\d*(?=</span>)')
a=0
b=2
jg=[]
for i in pattern.findall(f1txt)[30:]:
    if i !="Counter-Strike: Global Offensive":
        if "¥" in i:
            a+=1
            if a==2:
                a=0
                continue
        if b==2:
            b=0
            continue
        if "&" in i:
            break  
        jg.append(i)
        b+=1
#价格自动计算 jijia(zu,jg) return txt
jgb=[]
for k in jg:
    print(k)
    if "StatTrak" in k:
        continue
    if "破损" in k:
        jgb.append(float(jg[jg.index(k)-1].strip("¥ ")))
    if "战痕" in k:
        jgb.append(float(jg[jg.index(k)-1].strip("¥ ")))
    if "久经" in k:
        jgb.append(float(jg[jg.index(k)-1].strip("¥ ")))
    if "略有" in k:
        jgb.append(float(jg[jg.index(k)-1].strip("¥ ")))
    if "崭新" in k:
        jgb.append(float(jg[jg.index(k)-1].strip("¥ ")))
else:
    jgb.insert(0,jg[1].replace(" ","").strip(" ").split("(")[0])

#自定义配方组
while True:
    zu=input("请输入合成组名(不知道请填A):").upper()
    if(zu!=""):
        break
#清理空行   ql(zu)
last=1
with open("jgb"+zu+".txt","r") as f:
    ff=f.readlines()
fg=[]
for i in ff:
    fg.append(i.strip("\n"))
ff=open("jgb"+zu+".txt","w")
for i in range(len(fg)):
    if(len(fg[i])>1):
        ff.write(fg[i]+"\n")
ff.close()



#寻找最新序号
with open("jgb"+zu+".txt","r") as f:
    last=int(list(f.readlines()[-1])[1])+1
txt=zu+str(last)+"="+str(jgb)+"\n"
txt=txt.replace("'","\"").replace(", ",",").replace(" ","-")



#寻找曾是否出现 xieru(zu)
f12=open("jgb"+zu+".txt","r")
f1=f12.readlines()
f12.close()


print(txt)
pq=input("是否写入当前价格?(输入F取消):")
if(pq=="F"):
    quit()
    

xgm=0
for dqz in f1:
    if jgb[0] in dqz:
        print("修改前:"+dqz.strip("\n"))
        txt=txt.replace(txt.split("=")[0],dqz.split("=")[0]).strip("\n")
        print("修改后:"+txt)
        xz=input("是否修改?默认T,取消F:")
        if(xz=="F"):
            break
        f1[f1.index(dqz)]=txt
        f13=open("jgb"+zu+".txt","w")
        for i in f1:
            f13.write(i.strip("\n")+"\n")
        print("已写入")
        f13.close()
        xgm=1#return
        
#新建条目 new
if(xgm==0):
    print(txt)
    a=input("未找到相同值，是否写入新条目？(取消输入F):").upper()
    if(a!="F"):
        f12=open("jgb"+zu+".txt","a")
        f12.write(txt)
        f12.close()
        print("已写入")
